package com.armrest.screen.provider

import android.R.attr.y
import android.graphics.Typeface
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.ImageButton
import android.widget.ProgressBar
import android.widget.RelativeLayout
import android.widget.TextView
import com.armrest.screen.HomeMenuModel
import com.armrest.screen.ItemViewType
import com.armrest.screen.R
import com.armrest.screen.setMediaPlayer
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder


class ThermometerProvider(
    var layout: Int = R.layout.itme_thermometer_layout, var itemViewTypes:
    Int = ItemViewType.ITEM_TYPE_GRAVITY_MODEL
) : BaseItemProvider<HomeMenuModel>(), View.OnTouchListener { //,
    override val itemViewType: Int
        get() = itemViewTypes
    override val layoutId: Int
        get() = layout
    var temperature: Int = 24
    val onTouchNum=16
    var max: Float = 30f
    var min: Float = 16.0f
    var isOnTouch= true
    var progressBar: ProgressBar? = null
    var openOff: TextView? = null
    override fun convert(helper: BaseViewHolder, item: HomeMenuModel) {
        var button = helper.getViewOrNull<ImageButton>(R.id.button)
        var addBt = helper.getViewOrNull<ImageButton>(R.id.addBt)
        openOff = helper.getViewOrNull<TextView>(R.id.open_off)
        progressBar = helper.getViewOrNull<ProgressBar>(R.id.progressBar)
        progressBar?.let {
           it.progress =(temperature - onTouchNum)
        }
        var gravityLl = helper.getViewOrNull<RelativeLayout>(R.id.gravity_ll)
//        openOff?.let {
//            val typeface =
//                Typeface.createFromAsset(it.context.assets, "MicrosoftJhengHei-Light.ttf")
//            openOff?.setTypeface(typeface)
//        }
//        addBt?.setOnClickListener {
//            it.setMediaPlayer()
//            if (temperature < max) {
//                temperature ++
//                openOff?.text = temperature.toString()
//            } else {
////                Toast.makeText(it.context, "已经是最大", Toast.LENGTH_LONG).show()
//            }
//        }
//        button?.setOnClickListener {
//            it.setMediaPlayer()
//            if (temperature > min) {
//                temperature --
//                openOff?.text = temperature.toString()
//            } else {
////                Toast.makeText(it.context, "已经是最小", Toast.LENGTH_LONG).show()
//            }
//        }
        gravityLl?.setOnTouchListener(this)
    }

    var startY: Float = 0f //手指刚开始滑动时记录点 Y轴
    var startX: Float = 0f//手指刚开始滑动时记录点 X轴
    override fun onTouch(v: View, event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {//手指按下
                startY = event.y
                startX = event.x
            }
            MotionEvent.ACTION_MOVE -> {//手指滑动
                var endY = event.y
                var distanceY = endY - startY //滑动距离
                if (distanceY > 0) { //向下滑动
                    if (temperature > min &&isOnTouch) {
                        isOnTouch=false
                        temperature -= 1
                        setProgressBar((temperature - onTouchNum),v)
                    }
                } else if (distanceY < 0) {//向上滑动
                    if (temperature < max&& isOnTouch) {
                        temperature += 1
                        isOnTouch=false
                        setProgressBar((temperature - onTouchNum),v)
                    }
                }
            }
        }
        return true
    }
    fun setProgressBar(changedVolume: Int,v: View) {
        Thread {
            v.setMediaPlayer()
        }.start()
        progressBar?.let {
            progressBar?.postDelayed({
                it.progress = changedVolume
                openOff?.text = temperature.toString()
                isOnTouch=true
            }, 500)
        }
    }
}